package com.java110.mall.cart.cmd.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.housekeeping.HousekeepingResourceDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductGroupDto;
import com.java110.dto.product.ProductGroupOrderDto;
import com.java110.dto.product.ProductSeckillDto;
import com.java110.dto.product.ProductSeckillOrderDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderCartReturnDto;
import com.java110.mall.cart.bmo.businessQuery.*;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务查询
 */
@Java110Cmd(serviceCode = "business.businessQuery")
public class BusinessQueryCmd extends Cmd {

    @Autowired
    private IBusinessQueryVip businessQueryVipImpl;

    @Autowired
    private IBusinessQueryGoods businessQueryGoodsImpl;

    @Autowired
    private IBusinessQueryOrder businessQueryOrderImpl;

    @Autowired
    private IBusinessQueryRefund businessQueryRefundImpl;

    @Autowired
    private IBusinessQueryGroup businessQueryGroupImpl;

    @Autowired
    private IBusinessQuerySeckill businessQuerySeckillImpl;

    @Autowired
    private IBusinessQueryServ businessQueryServImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺编号");
        Assert.hasKeyAndValue(reqJson, "searchValue", "未包含搜索内容");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        String searchValue = reqJson.getString("searchValue");
        String shopId = reqJson.getString("shopId");

        JSONObject result = new JSONObject();
        List<ShopVipUserDto> shopVipUserDtos = new ArrayList<>();
        //todo 根据名称查会员
        businessQueryVipImpl.queryVipByName(reqJson,searchValue,shopId, shopVipUserDtos);
        // todo 根据手机号查询会员
        businessQueryVipImpl.queryVipByTel(reqJson,searchValue,shopId, shopVipUserDtos);
        result.put("shopVipUserDtos",shopVipUserDtos);

        List<ProductDto> productDtos = new ArrayList<>();
        // todo 根据商品名称查询商品
        businessQueryGoodsImpl.queryGoodsByName(reqJson,searchValue,shopId, productDtos);
        // todo 根据商品ID查询商品
        businessQueryGoodsImpl.queryGoodsById(reqJson,searchValue,shopId, productDtos);
        result.put("productDtos",productDtos);

        List<StoreOrderCartDto> storeOrderCartDtos = new ArrayList<>();
        // todo 根据商品名称查订单
        businessQueryOrderImpl.queryOrderByGoodsName(reqJson,searchValue,shopId, storeOrderCartDtos);
        // todo 根据订单号查询订单
        businessQueryOrderImpl.queryOrderByOrderId(reqJson,searchValue,shopId, storeOrderCartDtos);
        if(!ListUtil.isNull(shopVipUserDtos)) {
            // todo 根据会员查询订单
            businessQueryOrderImpl.queryOrderByVip(reqJson, shopVipUserDtos.get(0).getUserId(), shopId, storeOrderCartDtos);
        }
        result.put("storeOrderCartDtos",storeOrderCartDtos);

        List<StoreOrderCartReturnDto> storeOrderCartReturnDtos = new ArrayList<>();
        businessQueryRefundImpl.queryRefundId(reqJson, searchValue, shopId, storeOrderCartReturnDtos);

        if(!ListUtil.isNull(storeOrderCartDtos)) {
            businessQueryRefundImpl.queryRefundByCardId(reqJson, storeOrderCartDtos.get(0).getCartId(), shopId, storeOrderCartReturnDtos);
        }
        if(!ListUtil.isNull(shopVipUserDtos)) {
            // todo 根据会员查询订单
            businessQueryRefundImpl.queryRefundByVip(reqJson, shopVipUserDtos.get(0).getUserId(), shopId, storeOrderCartReturnDtos);
        }
        if(!ListUtil.isNull(productDtos)) {
            // todo 根据会员查询订单
            businessQueryRefundImpl.queryRefundByProduct(reqJson, productDtos.get(0).getProductId(), shopId, storeOrderCartReturnDtos);
        }

        result.put("storeOrderCartReturnDtos",storeOrderCartReturnDtos);

        List<ProductGroupDto> productGroupDtos = new ArrayList<>();
        //todo 查到了商品再去查
        if(!ListUtil.isNull(productDtos)) {
            //todo 根据商品名称查询拼团订单
            businessQueryGroupImpl.queryGroupByGoodsName(reqJson, searchValue, shopId, productGroupDtos);
            //todo 根据商品ID查询拼团商品
            businessQueryGroupImpl.queryGroupByProductId(reqJson, searchValue, shopId, productGroupDtos);
        }

        result.put("productGroupDtos",productGroupDtos);

        List<ProductGroupOrderDto> productGroupOrderDtos = new ArrayList<>();
        //todo 查到了商品再去查
        if(!ListUtil.isNull(productGroupDtos)) {
            //todo 根据商品ID查询拼团商品
            businessQueryGroupImpl.queryGroupOrderByGroupId(reqJson, productGroupDtos.get(0).getGroupId(), shopId, productGroupOrderDtos);
        }

        result.put("productGroupOrderDtos",productGroupOrderDtos);

        List<ProductSeckillDto> productSeckillDtos = new ArrayList<>();
        //todo 查到了商品再去查
        if(!ListUtil.isNull(productDtos)) {
            //todo 根据商品名称查询拼团订单
            businessQuerySeckillImpl.querySecKillByGoodsName(reqJson, searchValue, shopId, productSeckillDtos);
            //todo 根据商品ID查询拼团商品
            businessQuerySeckillImpl.querySecKillByProductId(reqJson, searchValue, shopId, productSeckillDtos);
        }
        result.put("productSeckillDtos",productSeckillDtos);

        List<ProductSeckillOrderDto> productSeckillOrderDtos = new ArrayList<>();
        if(!ListUtil.isNull(productSeckillDtos)) {
            //todo 根据商品ID查询拼团商品
            businessQuerySeckillImpl.querySeckillOrderByKillId(reqJson, productSeckillDtos.get(0).getKillId(), shopId, productSeckillOrderDtos);
        }
        result.put("productSeckillOrderDtos",productSeckillOrderDtos);

        List<HousekeepingServDto> housekeepingServDtos = new ArrayList<>();

        // todo 根据名称查询
        businessQueryServImpl.queryServByName(reqJson,searchValue,shopId,housekeepingServDtos);

        //todo 根据ID查询服务
        businessQueryServImpl.queryServById(reqJson,searchValue,shopId,housekeepingServDtos);

        result.put("housekeepingServDtos",housekeepingServDtos);

        List<HousekeepingResourceDto> housekeepingResourceDtos = new ArrayList<>();
        businessQueryServImpl.queryResourceByName(reqJson,searchValue,shopId,housekeepingResourceDtos);

        result.put("housekeepingResourceDtos",housekeepingResourceDtos);


        context.setResponseEntity(ResultVo.createResponseEntity(result));
    }
}
